package com.crm.system.controller.sys;

import com.crm.common.model.DataResponse;
import com.crm.common.model.DhResponse;
import com.crm.common.model.Enum.ResponseCodeEnum;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.entity.sys.permissionManagement.SysCompany;
import com.crm.model.qo.sys.CompanyQO;
import com.crm.model.vo.sys.CompanyVO;
import com.crm.system.service.permissionManagement.SysCompanyService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author yincuimei
 * @Date 2020/12/28 14:43
 */
@RestController
@RequestMapping("/sysCompany")
@Slf4j
public class SysCompanyController {
    @Autowired
    private SysCompanyService sysCompanyService;

    @GetMapping("/selectCompanies")
    public DataResponse getDeptNames(){
        try {
            List<Map<String, Object>> sysCompaniesMap = sysCompanyService.selectSysCompanySelectList();
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),sysCompaniesMap);
        } catch (Exception e) {
            log.error("sysCompany selectCompanies error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 查询公司拼接税盘编号
     * @return
     */
    @GetMapping("/selectCompanyAndTaxNumberList")
    public DataResponse selectCompanyAndTaxNumberList(){
        try {
            List<Map<String, Object>> sysCompaniesMap = sysCompanyService.selectCompanyAndTaxNumberList();
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),sysCompaniesMap);
        } catch (Exception e) {
            log.error("sysCompany selectCompanyAndTaxNumberList error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 根据公司查询公司税号
     * @return
     */
    @GetMapping("/selectCompanyTaxNumber")
    public DataResponse selectCompanyTaxNumber(@RequestParam String companyId){
        try {
            String taxNumber = sysCompanyService.selectCompanyTaxNumber(companyId);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),taxNumber);
        } catch (Exception e) {
            log.error("sysCompany selectCompanyAndTaxNumberList error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /***
     * @Description:公司管理-查询公司列表
     * @Param:
     * @Author: luojie
     * @Date: 2021/5/19 15:38
     */
    @PostMapping("/selectCompanyList")
    public DataResponse selectCompanyList(@RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                          @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                          @RequestParam(value = "companyName", required = true) String companyName ){
        List<CompanyVO> companyVOList = sysCompanyService.selectCompanyList(pageIndex,pageSize,companyName);
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),companyVOList);
    }

    /***
     * @Description:添加公司信息
     * @Param: companyQO 公司对象
     * @Author: luojie
     * @Date: 2021/5/19 17:55
     */
    @PostMapping("/addCompany")
    public DataResponse addCompany(@RequestBody CompanyQO companyQO){
        //查询是否有相同公司
        String companyName = companyQO.getCompanyName();
        SysCompany sysCompanySelect = sysCompanyService.selectSysCompanyByCompanyName(companyName);
        if(sysCompanySelect==null){
            Boolean result = sysCompanyService.addCompany(companyQO);
            if(result){
                return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
            }else {
                return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
            }
        }else {
            return new DhResponse(ResponseCodeEnum.COMPANY_NAME_ALREADY_EXISTS.getResponseCode());
        }
    }



    /**
     * @author  yuanyunfeng
     * @Description 当前登录人信息
     * @date  2021/12/2 11:08
     * @Param []
     * @return com.crm.common.model.DataResponse
     **/
    @GetMapping("/currentInfo")
    public DataResponse getCurrentInfo(){
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();//当前登录人信息
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),userAccount);
    }

}
